<template>
  <div class="form-email">
    <div class="tx-btn text-blue-dark mb-1">{{ title }}</div>
    <div class="form-email-input">
      <input
        type="email"
        placeholder="example@yourmail.com"
        v-model="myValue"
        class="default-input"
        :class="{ 'has-error': myError.length > 0 }"
        @change="checkError"
      />
      <div class="input-error" v-if="myValue">
        请输入正确的格式
      </div>
      <div class="input-error" v-else>
        此为必填项
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, nextTick } from 'vue';

defineOptions({
  name: "FormEmail"
});

interface Props {
  value?: string;
  title?: string;
  componentKey?: string;
}

const props = withDefaults(defineProps<Props>(), {
  value: "",
  title: "",
  componentKey: ""
});

const emit = defineEmits<{
  updateValue: [value: string];
  updateError: [error: { key: string; isPass: boolean }];
}>();

const myError = ref<string[]>([]);

const myValue = computed({
  get() {
    return props.value;
  },
  set(nVal: string) {
    emit("updateValue", nVal);
  },
});

const validateEmail = (inputText: string): boolean => {
  const mailformat =
    /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
  return !!inputText.match(mailformat);
};

const checkError = () => {
  const key = "myNumber";
  nextTick(() => {
    const isValid = validateEmail(myValue.value);
    if (!isValid && myError.value.indexOf(key) < 0)
      myError.value.push(key);
    if (isValid && myError.value.indexOf(key) > -1)
      myError.value.splice(myError.value.indexOf(key), 1);
    emit("updateError", {
      key: props.componentKey,
      isPass: myError.value.length <= 0,
    });
  });
};
</script>

<style lang="scss" scoped>
.tx-btn {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.285;
  letter-spacing: .04em
}

@media(max-width: 1023.98px) {
  .tx-btn {
      line-height:1.428
  }
}

@media(max-width: 767.98px) {
  .tx-btn {
      line-height:1.285
  }
}

.text-blue-dark {
  --bs-text-opacity: 1;
  color: rgba(0,50,160,var(--bs-text-opacity))!important;
}

.mb-1 {
  margin-bottom: 4px!important
}

input.default-input,textarea.default-textarea {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #1a1c20;
  background: none;
  background: #f7f7fa;
  border-radius: 4px;
  border: none;
  padding: 8px 12px;
  outline: none;
  width: 100%
}

@media(max-width: 1023.98px) {
  input.default-input,textarea.default-textarea {
      font-size:14px;
      line-height: 1.714;
      letter-spacing: .01em
  }
}

@media(max-width: 767.98px) {
  input.default-input,textarea.default-textarea {
      background:#fff
  }
}

input.default-input:focus,textarea.default-textarea:focus {
  padding: 7px 11px;
  border: 1px solid #0048e8
}

input.default-input::-moz-placeholder,textarea.default-textarea::-moz-placeholder {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #c0c0c4
}

input.default-input:-ms-input-placeholder,textarea.default-textarea:-ms-input-placeholder {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #c0c0c4
}

input.default-input::placeholder,textarea.default-textarea::placeholder {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #c0c0c4
}

@media(max-width: 1023.98px) {
  input.default-input::-moz-placeholder,textarea.default-textarea::-moz-placeholder {
      font-size:14px;
      line-height: 1.714;
      letter-spacing: .01em
  }

  input.default-input:-ms-input-placeholder,textarea.default-textarea:-ms-input-placeholder {
      font-size: 14px;
      line-height: 1.714;
      letter-spacing: .01em
  }

  input.default-input::placeholder,textarea.default-textarea::placeholder {
      font-size: 14px;
      line-height: 1.714;
      letter-spacing: .01em
  }
}

input.default-input.has-error,textarea.default-textarea.has-error {
  padding: 6px 10px;
  border: 2px solid #f04
}

input.default-input.has-error+.input-error,textarea.default-textarea.has-error+.input-error {
  opacity: 1;
  position: relative
}

input.default-input+.input-error,textarea.default-textarea+.input-error {
  position: absolute;
  margin-top: 4px;
  color: #ce031a;
  opacity: 0;
  transition: opacity .15s ease-in-out;
  pointer-events: none
}

.form-email {
  margin-bottom: 36px;
}

@media (max-width: 767.98px) {
  .form-email {
    margin-bottom: 24px;
  }
}
</style>